package gof_001_创建型_Create.gof_001_单例_Singleton.gof_010_Reflect;

import java.lang.reflect.Constructor;

/**
 * 反射破坏单例
 */
public class ReflectMain {
    public static void main(String[] args) throws Exception {
        Student student = Student.getInstance();
        System.out.println("第一次获得的 student = " + student);


        Class<? extends Student> aClass = null;
        aClass = student.getClass();
        //aClass =  (Class<? extends Student>) Class.forName("gof_01_Create.gof_001_Singleton.gof_010_Reflect.Student");

        //获得 Student 类信息

        //获得空构造器
        Constructor<? extends Student> constructor = aClass.getDeclaredConstructor(null);

        //越过访问权限
        constructor.setAccessible(true);
        Student student1 = constructor.newInstance();
        constructor.setAccessible(false);

        //生成的对象是属于单例类的,但是不是单例
        System.out.println("新生成的实例是否属于 Student 实例: " + (student1 instanceof Student));
        System.out.println("第二次获得的 student = " + student1);
    }
}
